// pages/dev_manager/dev_manager.js
var app = getApp()
var header = {
  'content-type': 'application/x-www-form-urlencoded',
  'cookie': "openid=" + app.globalData.openid
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tree_district:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.request({
      url: 'https://119.3.46.145/server/loadTree?userid=' + app.globalData.userid,  //获取区域列表构造树
      header: header,
      method:'post',
      success: function (res) {
        // console.log(res, "res");
        //[{"attributes":{"mid":1,"type":0},"id":100000001,"text":"测试区域2","state":"closed","iconCls":"area_img"}]
        let data = JSON.parse(res.data.data);

        //当前没有区域 []
        if (data.length == 0) {
          return;
        }

        //当前有区域[{"attributes":{"mid":1,"type":0},"id":100000001,"text":"测试区域2","state":"closed","iconCls":"area_img"}]
        let tmp_distrcit = [];
        data.forEach(function (item, index, self) {
          let child_district = {
            text: '',
            mid: '',
            type: '',
            id: '',
            state: ''
            // path:'treeData.childMenus['+index+']'
          };
          child_district.text = item.text;
          child_district.mid = item.attributes.mid;
          child_district.type = item.attributes.type;
          child_district.id = item.id;
          child_district.state = item.state;
          tmp_distrcit.push(child_district);
        });      

        that.setData({
          tree_district: tmp_distrcit
        });

        //console.log(that.data.tree_district,"onload tree_district");
      },
      fail: function (error) {
        console.log(error, "dev_manager  onload loadTree");
      }
    });

    

  },

  add_district:function(){
    let that = this;
    wx.navigateTo({
      url: '../new_district/new_district',
    })
  },

  mod_district:function(e){
    let that = this;
    //console.log(e, 'e')
    let mid = e.currentTarget.dataset.mid;
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../edit_district/edit_district?id='+mid
    })
  },

  del_district:function(e){
    let that = this;

    //console.log(that.data.tree_district, "del tree_district");
   
    let mid = e.currentTarget.dataset.mid;
    let id = e.currentTarget.dataset.id;
    wx.request({
      url: 'https://119.3.46.145/server/DeleteDepartment?id=' + mid,
      header:header,
      method:'post',
      success:res=>{
        //console.log(res.data, "res");
        //data: {error: "0", info: "删除成功"}
        wx.showModal({
          title: '提示',
          content: res.data.info,
          showCancel:false,
          success:function(){
            //"0"删除成功
            //console.log(res.data.error=="0");
            if(res.data.error == "0"){
              let tmp_tree_district = that.data.tree_district;
              try {
                tmp_tree_district.forEach(function (item, index, self) {
                  if (item.mid == mid) {
                    self.splice(index, 1);
                    // console.log(index, "index");
                    throw new Error("ending");//报错，就跳出循环
                  }
                  // console.log("test foreach");
                });               
              } catch (e) {
                if (e.message == "ending") {
                  // log("结束了");
                }
              }
             
              that.setData({
                tree_district: tmp_tree_district
              });
            }
          }
           
        });

      },
      fail:error=>{
        console.log(error, "DeleteDepartment error");
      }
    })
  }, 

  list_loadgroup:function(e){
    let that = this;
    let district_info = e.currentTarget.dataset.detail;

    wx.navigateTo({
      url: '../loadgroup_manager/loadgroup_manager?district_info=' + JSON.stringify(district_info)
    }); 
  
  //此处id为1000000001之类的
    // wx.request({
    //   url: 'https://119.3.46.145/server/loadTree?userid=' + app.globalData.userid + '&id=' + district_info.id,
    //   method:'post',
    //   header:header,
    //   success:res=>{
        //{"data":"[{\"attributes\":{\"mid\":1,\"type\":1},\"id\":200000001,\"text\":\"测试路段\",\"state\":\"closed\",\"iconCls\":\"loadgroup_img\"},{\"attributes\":{\"mid\":4,\"type\":1},\"id\":200000004,\"text\":\"测试路段2\",\"state\":\"closed\",\"iconCls\":\"loadgroup_img\"},{\"attributes\":{\"mid\":6,\"type\":1},\"id\":200000006,\"text\":\"测试路段3\",\"state\":\"closed\",\"iconCls\":\"loadgroup_img\"}]"}
    //     console.log(res, "list_loadgroup loadTree");
    //     let data = JSON.parse(res.data.data);
       
    //     wx.navigateTo({
    //       url: '../loadgroup_manager/loadgroup_manager?district_info=' + JSON.stringify(district_info)+'&loadgroup_list='+res.data.data
    //     });        
    //   },
    //   fail:error=>{

    //   }
    // })

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

 

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})